package cn.iocoder.yudao.module.system.controller.admin.mini.orderInfo.logistics.controller.admin.logistics;

import cn.iocoder.yudao.module.system.controller.admin.mini.orderInfo.logistics.controller.admin.logistics.vo.*;
import cn.iocoder.yudao.module.system.controller.admin.mini.orderInfo.logistics.convert.logistics.LogisticsConvert;
import cn.iocoder.yudao.module.system.controller.admin.mini.orderInfo.logistics.dal.dataobject.logistics.LogisticsDO;
import cn.iocoder.yudao.module.system.controller.admin.mini.orderInfo.logistics.dal.mysql.logistics.LogisticsMapper;
import cn.iocoder.yudao.module.system.controller.admin.mini.orderInfo.logistics.service.logistics.LogisticsService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - 小程序-物流公司")
@RestController
@RequestMapping("/qd/logistics")
@Validated
public class LogisticsController {

    @Resource
    private LogisticsService logisticsService;

    @PostMapping("/create")
    @Operation(summary = "创建小程序-物流公司")
    @PreAuthorize("@ss.hasPermission('qd:logistics:create')")
    public CommonResult<Integer> createLogistics(@Valid @RequestBody LogisticsCreateReqVO createReqVO) {
        return success(logisticsService.createLogistics(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新小程序-物流公司")
    @PreAuthorize("@ss.hasPermission('qd:logistics:update')")
    public CommonResult<Boolean> updateLogistics(@Valid @RequestBody LogisticsUpdateReqVO updateReqVO) {
        logisticsService.updateLogistics(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除小程序-物流公司")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('qd:logistics:delete')")
    public CommonResult<Boolean> deleteLogistics(@RequestParam("id") Integer id) {
        logisticsService.deleteLogistics(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得小程序-物流公司")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('qd:logistics:query')")
    public CommonResult<LogisticsRespVO> getLogistics(@RequestParam("id") Integer id) {
        LogisticsDO logistics = logisticsService.getLogistics(id);
        return success(LogisticsConvert.INSTANCE.convert(logistics));
    }

    @Resource
    LogisticsMapper logisticsMapper;

    @GetMapping("/getList")
    public CommonResult getList(){
        List<LogisticsDO> list=new ArrayList<>();
        for (LogisticsDO dto : logisticsMapper.selectList()) {
            list.add(dto);
        }
        return success(list);
    }

    @GetMapping("/list")
    @Operation(summary = "获得小程序-物流公司列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('qd:logistics:query')")
    public CommonResult<List<LogisticsRespVO>> getLogisticsList(@RequestParam("ids") Collection<Integer> ids) {
        List<LogisticsDO> list = logisticsService.getLogisticsList(ids);
        return success(LogisticsConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得小程序-物流公司分页")
    @PreAuthorize("@ss.hasPermission('qd:logistics:query')")
    public CommonResult<PageResult<LogisticsRespVO>> getLogisticsPage(@Valid LogisticsPageReqVO pageVO) {
        PageResult<LogisticsDO> pageResult = logisticsService.getLogisticsPage(pageVO);
        return success(LogisticsConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出小程序-物流公司 Excel")
    @PreAuthorize("@ss.hasPermission('qd:logistics:export')")
    @OperateLog(type = EXPORT)
    public void exportLogisticsExcel(@Valid LogisticsExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<LogisticsDO> list = logisticsService.getLogisticsList(exportReqVO);
        // 导出 Excel
        List<LogisticsExcelVO> datas = LogisticsConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "小程序-物流公司.xls", "数据", LogisticsExcelVO.class, datas);
    }

}
